package OfferAliBaBa.a_21年阿里笔试;

import java.util.Scanner;

/**
 * @author 王英龙(WangYingLong)
 * @version 1.0
 * @date 2023-10-23 18:02
 * @description LeetcodeLearnAlgorithm -> OfferAliBaBa.a_21年阿里笔试
 */
public class Main2a {
    static long mod = (long)Math.pow(10,9)+7;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        for(int i=0;i<m;i++){
            long a = in.nextInt();
            long b = in.nextInt();
            int n = in.nextInt();
            a %= mod;
            b %= mod;
            System.out.println(solve(a,b,n));
        }
    }

    public static long solve(long a, long b,int n){
        long[] nums = new long[n+1];
        nums[1] = a;
        if(n==1) return nums[1];
        nums[2] = (a * a % mod - 2 * b % mod + mod) % mod;
        if(n==2) return nums[2];
        for(int i=3;i<=n;i++)
            nums[i] = ((a*nums[i-1]%mod)-(b*nums[i-2]%mod)+mod)%mod;
        return nums[n];
    }
}
